package com.boss.ccs.gis;

import com.boss.Messages;
import com.boss.core.BossConstants;
import com.boss.core.form.BossForm;
import com.boss.core.form.BossFormHelper;
import com.boss.core.manage.BossProperty;
import com.boss.core.manage.PropertySection;
import com.boss.core.util.BossGlobalUtil;
import com.boss.core.util.BossUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;

import java.util.List;
import java.util.Map;

/**
 * Boss GIS2 Form (online-help)
 * @author Momin Noor Khan
 */
public class BossGIS2Form extends BossForm {
    private boolean isTargetSection;
    private String targetProfile;
    private String customer = "";
    private String language = "";
    private String helpArtifacts = "";

    //Getters and Setters
    public boolean isTargetSection() {
        return isTargetSection;
    }

    public void setTargetSection(boolean targetSection) {
        isTargetSection = targetSection;
    }

    public String getTargetProfile() {
        return targetProfile;
    }

    public void setTargetProfile(String targetProfile) {
        this.targetProfile = targetProfile;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getHelpArtifacts() {
        return helpArtifacts;
    }

    public void setHelpArtifacts(String helpArtifacts) {
        this.helpArtifacts = helpArtifacts;
    }

    public BossGIS2Form(StaplerRequest req, JSONObject jsonForm, String project) {
        super(req, jsonForm, project);
        setRelease(BossUtil.getStringValueFromJson(getJsonTaskSection(), BossConstants.KEY_RELEASE));
        customer = BossUtil.getStringValueFromJson(getJsonTaskSection(), BossConstants.KEY_CUSTOMER);
        language = BossUtil.getStringValueFromJson(getJsonTaskSection(), BossConstants.KEY_LANGUAGE);
        helpArtifacts = getSelectedHelpArtifacts(getTask(), getJsonTaskSection());
        isTargetSection = BossUtil.contains(getJsonTaskSection(), BossConstants.KEY_TARGET);
        targetProfile = BossFormHelper.getGisTargetProfile(isTargetSection, req);
    }

    @Override
    public void prepareUrlParamMap() {
        super.prepareUrlParamMap();
        Map<String, String> map = getUrlMap();
        map.put(BossConstants.URL_PARAM_CUSTOMER, customer);
        map.put(BossConstants.URL_PARAM_LANGUAGE, language);
        map.put(BossConstants.URL_PARAM_HELP_ARTIFACTS, helpArtifacts);
        map.put(BossConstants.URL_PARAM_TARGET_PROFILE, targetProfile);
        setUrlMap(map);
    }

    public String getSelectedHelpArtifacts(String task, JSONObject jsonTaskSection) {
        String helpArtifacts = "";
        if (task.equals(BossConstants.TASK_COPY_HELP)) {
            List<BossProperty> artifacts = getAllHelpArtifacts();
            for (BossProperty artifact : artifacts) {
                if (jsonTaskSection != null && jsonTaskSection.has(artifact.getName())) {
                    if (jsonTaskSection.getBoolean(artifact.getName())) {
                        helpArtifacts = helpArtifacts + StringUtils.strip(artifact.getName()) + ",";
                    }
                }
            }
            helpArtifacts = StringUtils.removeEnd(helpArtifacts, ",");
        }
        return helpArtifacts;
    }

    public List<BossProperty> getAllHelpArtifacts() {
        PropertySection propertySection = BossGlobalUtil.getPropertySection(BossConstants.ONLINE_HELP_ARTIFACTS_KEY);
        List<BossProperty> properties = propertySection.getProperties();
        return properties;
    }

    @Override
    public boolean isValid() {
        boolean valid = true;
        if (!checkMandatoryFields() |
                !checkTargetProfile() |
                !checkHelp()
                ) {
            valid = false;
        } else if (!BossFormHelper.checkAvailableJob(getRequest(), getAvailableJob())) {
            valid = false;
        }
        return valid;
    }

    // Validation methods
    public boolean checkMandatoryFields() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        if (!checkRelease() |
                !BossFormHelper.checkTask(req, task)) {
            valid = false;
        }
        return valid;
    }

    public boolean checkRelease() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        String release = getRelease();
        if (task.equals(BossConstants.TASK_COPY_HELP)) {
            if (BossUtil.isBlankOrDummy(release)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryRelease());
                valid = false;
            }
        }
        return valid;
    }

    public boolean checkHelp() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        String language = getLanguage();
        String helpArtifacts = getHelpArtifacts();
        if (task.equals(BossConstants.TASK_COPY_HELP)) {
            if (BossUtil.isBlankOrDummy(language)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryLanguage(task));
                valid = false;
            }

            if (StringUtils.isBlank(helpArtifacts)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryHelpArtifact(task));
                valid = false;
            }
        }
        return valid;
    }

    public boolean checkTargetProfile() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        if (task.equals(BossConstants.TASK_COPY_HELP)) {
            if (StringUtils.isBlank(targetProfile)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryTarget(task));
                valid = false;
            }
        }
        return valid;
    }
}
